/**
 * Malsha 2014-10-29
 */

CancellationProcess = new Object();

CancellationProcess.getCenterPanel = function() {

	var confirm_investments = Ext.create('Ext.data.Store', {
		fields : [ 'id', 'invenstmentCode', 'amount', 'status', 'customer', 'customer_code', 'customer_name', 'fund_id', 'confirmDate', 'unitPrice', 'noOfUnits' ]
	});

	var confirm_redemption = Ext.create('Ext.data.Store', {
		fields : [ 'id', 'redemptionCode', 'totAmount', 'status', 'customer', 'customerCode', 'customer_name', 'fund_id', 'confirmDate' ]
	});

	grid = Ext.create('Ext.grid.Panel', {
		title : 'Investments',
		store : confirm_investments,
		height : 300,
		plugins : [ Ext.create('Ext.grid.plugin.CellEditing', {
			clicksToEdit : 1
		}) ],
		columns : [ {
			header : 'Customer Code',
			dataIndex : 'customer_code',
			width : 100
		}, {
			header : 'Investment Code',
			dataIndex : 'invenstmentCode',
			width : 100
		}, {
			header : 'Amount',
			dataIndex : 'amount',
			width : 125,
			flex : 1,
			xtype : 'numbercolumn',
			format : '0,000.0000',
			align : 'right'
		}, {
			xtype : 'actioncolumn',
			align : 'center',
			width : 80,
			items : [ {
				icon : 'images/Cancel.png',
				handler : function(grid, rowIndex, colIndex) {
					var rec = grid.getStore().getAt(rowIndex);
					if (rec.get('id') != null) {
						Ext.MessageBox.confirm('Confirm', 'Are you sure you want to cancel this?', function(btn) {
							if (btn == 'yes') {
								var myMask = new Ext.LoadMask(formpanel, {
									msg : "Please wait..."
								});
								myMask.show();
								Ext.Ajax.request({
									url : 'cancelInvestment.action',
									params : {
										"investment_id" : rec.get('id'),
									},
									success : function(response) {
										var obj = Ext.decode(response.responseText);
										confirm_investments.remove(rec);
										myMask.hide();
									}
								});
							}
						});
					}
				}
			} ]
		} ]
	});
	redemptiongrid = Ext.create('Ext.grid.Panel', {
		title : 'Redemptions',
		store : confirm_redemption,
		height : 300,
		plugins : [ Ext.create('Ext.grid.plugin.CellEditing', {
			clicksToEdit : 1
		}) ],
		columns : [ {
			header : 'Customer Code',
			dataIndex : 'customerCode',
			width : 100
		}, {
			header : 'Redemption Code',
			dataIndex : 'redemptionCode',
			width : 100
		}, {
			header : 'Amount',
			dataIndex : 'totAmount',
			width : 125,
			flex : 1,
			xtype : 'numbercolumn',
			format : '0,000.0000',
			align : 'right'
		}, {
			xtype : 'actioncolumn',
			align : 'center',
			width : 80,
			items : [ {
				icon : 'images/Cancel.png',
				handler : function(grid, rowIndex, colIndex) {
					var rec = grid.getStore().getAt(rowIndex);
					if (rec.get('id') != null) {
						Ext.MessageBox.confirm('Confirm', 'Are you sure you want to cancel this?', function(btn) {
							if (btn == 'yes') {
								var myMask = new Ext.LoadMask(formpanel, {
									msg : "Please wait..."
								});
								myMask.show();
								Ext.Ajax.request({
									url : 'cancelRedemption.action',
									params : {
										"redemption_id" : rec.get('id'),
									},
									success : function(response) {
										var obj = Ext.decode(response.responseText);
										confirm_redemption.remove(rec);
										myMask.hide();
									}
								});
							}
						});
					}
				}
			} ]
		} ]
	});

	return formpanel = Ext.create('Ext.form.Panel', {
		title : 'Confirmed Investment / Redemption Cancellation',
		bodyPadding : 10,
		autoScroll : true,
		frame : true,
		defaults : {
			labelWidth : 110,
			anchor : '65%',
		},
		items : [ grid, redemptiongrid ],
		listeners : {
			render : function(p) {
				Ext.Ajax.request({
					url : 'getconfirmInvestment.action',
					success : function(response) {
						var obj = Ext.decode(response.responseText);
						confirm_investments.loadRawData(obj.investmentBeans);
					}
				});
				Ext.Ajax.request({
					url : 'getConfRedemptions.action',
					success : function(response) {
						var obj = Ext.decode(response.responseText);
						confirm_redemption.loadRawData(obj.redemptionDtos);
					}
				});
			}
		}
	});

};